using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Collections;

namespace Home.Controls
{
    public class TreeListNode : TreeNode
    {

        public TreeListNode()
        {
            m_Child = new List<string>();
            m_Data = new TreeListNodeData();
            
        }
        private List<string> m_Child;
        internal List<string> Child
        {
            get
            {
                return m_Child;
            }
            set
            {
                m_Child = value;
            }
        }

        public string Children
        {
            get
            {
                return string.Join(",", m_Child.ToArray());
            }
        }

        public string TreeTr
        {
            get
            {
                return string.Format("id=\"{0}\" depth=\"{1}\" childids=\"{2}\" style=\"{3}\"",this.DisplayID,base.Depth,this.Children,IsShowNodeStyle());
            }
        }

        public string TreeImage
        {
            get
            {
                TreeLineMaker make = new TreeLineMaker();
                make.Depth = TreeListView.IsShowRootNode ? this.Depth : this.Depth-1;
                make.HasChild = this.ChildNodes.Count != 0;
                make.ChildNumber = this.ChildNodes.Count;
                make.ImageUrl = this.ImageUrl;
                make.IsExpand = this.Expanded;
                make.IsShowLines = TreeListView.IsShowLines;
                make.Text = this.Text;
                make.IsLastNode = this.IsLastNode;
                make.IsFirstNode = this.ID==0 || (this.ID==1 && !TreeListView.IsShowRootNode);

                make.ImageCollapse = TreeListView.ImageCollapse;
                make.ImageExpand = TreeListView.ImageExpand;
                make.ImageHorizontal = TreeListView.ImageHorizontal;
                make.ImageVertial = TreeListView.ImageVertial;

                make.TreeListNode = this;

                if (Parent!=null)
                {
                    make.IsParentLastNode = ((TreeListNode)Parent).IsLastNode;
                }
               

                return make.Make();
            }
        }

        private object m_DataSource;
        public object DataSource
        {
            get
            {
                return m_DataSource;
            }
            set
            {
                m_DataSource = value;
            }
        }

        private TreeListNodeData m_Data;

        /// <summary>
        /// TreeListNode's Data (TreeListNode.TreeListNodeData)
        /// </summary>
        internal TreeListNodeData Data
        {
            get
            {
                return m_Data;
            }
            set
            {
                m_Data = value;
            }
        }

        private string m_ImageURL;
        internal string ImageURL
        {
            get
            {
                return m_ImageURL;
            }
            set
            {
                m_ImageURL = value;
            }
        }

        private int m_ID;
        public int ID
        {
            get
            {
                return m_ID;
            }
            set
            {
                m_ID = value;
            }
        }

        private string m_DisplayID;
        public string DisplayID
        {
            get
            {
                return m_DisplayID;
            }
            set
            {
                m_DisplayID = value;
            }
        }

        private TreeListView m_TreeListView = null;
        internal TreeListView TreeListView
        {
            get
            {
                return m_TreeListView;
            }
            set
            {
                m_TreeListView = value;
            }
        }

        private bool m_IsLastNode = false;
        public bool IsLastNode
        {
            get
            {
                return m_IsLastNode;
            }
            set
            {
                m_IsLastNode = value;
            }
        }

        private string IsShowNodeStyle()
        {
            if (this.Parent==null)
            {
                if (TreeListView.IsShowRootNode)
                {
                    return string.Empty;
                }
                else
                {
                    return "display:none";
                }
            }
            if (this.Parent.Expanded == null)
            {
                int depth = TreeListView.IsShowRootNode ? this.Depth : this.Depth - 1;
                if (depth > TreeListView.ExpandDepth)
                {
                    return "display:none";
                }
                else
                {
                    return string.Empty;
                }
            }
            if (this.Expanded == null)
            {
                int depth = TreeListView.IsShowRootNode ? this.Depth : this.Depth - 1;
                if (depth < TreeListView.ExpandDepth)
                {
                    this.Expanded = this.Parent.Expanded;
                }                
            }
            if (this.Parent.Expanded.Value)
            {
                return string.Empty;
            }
            else
            {
                return "display:none";
            }
        }

        public void SetNodeValue(string strKey, string strValue)
        {
            Data.SetValue(strKey, strValue);
        }
    }
}
